package firewing.util;

import java.util.Date;
import java.util.Random;

/**
 * used to create names for ad groups and campaigns
 * 
 * @author Shuai Yuan
 */
public final class RandomTokenGenerator {

	private static final int DICT_LENGTH = 36;

	private static final char[] dict = new char[DICT_LENGTH];

	private final int DEFAULT_LENGTH = 8;

	static {
		for (int i = 0; i < 10; i++) {
			dict[i] = (char) ('0' + i);
		}

		for (int i = 0; i < 26; i++) {
			dict[i + 10] = (char) ('A' + i);
		}
	}

	private final Random random = new Random((new Date()).getTime());

	private final char[] buff;

	public RandomTokenGenerator(int length) {
		buff = new char[length];
	}

	public RandomTokenGenerator() {
		buff = new char[DEFAULT_LENGTH];
	}

	public String nextToken() {
		for (int i = 0; i < buff.length; i++) {
			buff[i] = dict[random.nextInt(DICT_LENGTH)];
		}

		return new String(buff);
	}
}
